package com.example.controller;

import com.example.entity.Page.PageRequest;
import com.example.entity.Unit;
import com.example.result.CommonResult;
import com.example.service.UnitService;
import com.example.utils.redis.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @project: smart-property
 * @description: 单元管理 controller 层
 * @author: cjq
 * @create: 2022-11-18 10:50
 **/
@RestController
@RequestMapping("/unit")
public class UnitController {
    @Autowired
    UnitService unitService;

    @Autowired
    RedisUtil redisUtil;

    Integer cid;

    /**
     * 单元名称查重
     * @param unit
     * @param bid
     * @return
     */
    @GetMapping("/listUnit")
    public CommonResult listUnit(String unit,Integer bid){
        return CommonResult.ok(unitService.listUnit(unit,bid));
    }

    /**
     * 添加/修改单元信息
     * @param unit
     * @return
     */
    @PostMapping("/saveUnitInfo")
    public CommonResult saveUnitInfo(@RequestBody Unit unit){
        String houseInfo ="houseInfo_"+this.cid;
        redisUtil.del(houseInfo);
        return CommonResult.ok(unitService.saveUnitInfo(unit));
    }

    /**
     * 单元列表展示
     * @param bid
     * @param unit
     * @param pageRequest
     * @return
     */
    @GetMapping("/queyUnitInfo")
    public CommonResult queyUnitInfo(Integer bid, Integer cid,String unit, PageRequest pageRequest){
        return CommonResult.ok(unitService.queyUnitInfo(bid,cid,unit,pageRequest));
    }

    /**
     * 删除单元信息
     * @param id
     * @return
     */
    @DeleteMapping("/delUnitInfo")
    public CommonResult delUnitInfo(Integer id){
        String houseInfo ="houseInfo_"+this.cid;
        redisUtil.del(houseInfo);
        return CommonResult.ok(unitService.delUnitInfo(id));
    }

    /**
     * 批量删除单元信息
     * @param ids
     * @return
     */
    @PutMapping("/batchDel/{ids}")
    public CommonResult batchDel(@PathVariable Integer[] ids){
        String houseInfo ="houseInfo_"+this.cid;
        redisUtil.del(houseInfo);
        return CommonResult.ok(unitService.batchDel(ids));
    }
}
